package cn.cdeden.aiqa.controller.llmApp;


import cn.cdeden.aiqa.domain.bo.*;
import cn.cdeden.aiqa.domain.vo.*;
import cn.cdeden.aiqa.service.DifyService;
import cn.cdeden.common.core.domain.R;
import cn.cdeden.common.web.core.BaseController;
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import reactor.core.publisher.Flux;

import java.io.IOException;

/**
 * Dify接口对接
 * 前端访问路由地址为:/aiqa/dify
 *
 * @author Nick
 * @date 2024-12-08
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/dify")
public class DifyController extends BaseController {

    private final DifyService difyService;

    /**
     * 通过文本创建文档
     */
    @PostMapping("/datasets/{datasetId}/document/create-by-text")
    public DifyCreateByTextVo createByText(@PathVariable String datasetId, @Validated @RequestBody DifyCreateByTextBo bo) {
        return difyService.createByText(datasetId,bo);
    }

    /**
     * 通过文件创建文档
     */
    @PostMapping("/datasets/{datasetId}/document/create-by-file")
    public DifyCreateByFileVo createByFile(@RequestParam("file") MultipartFile file, @PathVariable String datasetId, @Validated @RequestBody DifyCreateByFileBo bo) throws IOException {
        return difyService.createByFile(file,file.getOriginalFilename(),datasetId,bo);
    }

    /**
     * 查询知识库列表
     */
    @GetMapping("/datasets")
    public DifyKnowledgeBaseVo list(@NotNull(message = "当前页不能为空")Integer page, Integer limit) {
        return difyService.datasets(page, limit);
    }

    /**
     * 创建空知识库
     */
    @PostMapping("/datasets")
    public DifyKnowledgeBaseCreateVo create(@Validated @RequestBody DifyKnowledgeBaseBo bo) {
        return difyService.create(bo);
    }

    /**
     * 删除空知识库
     */
    @DeleteMapping("/datasets/{datasetId}")
    public  R<Void> delete(@PathVariable String datasetId) {
        return toAjax(difyService.del(datasetId));
    }

    /**
     * 通过文本更新文档
     */
    @PostMapping("/datasets/{datasetId}/documents/{documentId}/update-by-text")
    public DifyUpdateByTextVo updateByText(@PathVariable String datasetId,@PathVariable String documentId, @Validated @RequestBody DifyUpdateByTextBo bo) {
        return difyService.updateByText(datasetId,documentId,bo);
    }

    /**
     * 通过文件更新文档
     */
    @PostMapping("/datasets/{datasetId}/documents/{documentId}/update-by-file")
    public DifyUpdateByFileVo updateByFile(@RequestParam("file") MultipartFile file, @PathVariable String datasetId,@PathVariable String documentId, @Validated @RequestBody DifyUpdateByFileBo bo) {
        return difyService.updateByFile(file,datasetId,documentId,bo);
    }

    /**
     * 获取文档嵌入状态（进度）
     */
    @GetMapping("/datasets/{datasetId}/documents/{batch}/indexing-status")
    public DifyIndexingStatusVo indexingStatus(@PathVariable String datasetId,@PathVariable String batch) {
        return difyService.indexingStatus(datasetId,batch);
    }

    /**
     * 删除文档
     */
    @DeleteMapping("/datasets/{datasetId}/documents/{documentId}")
    public  R<Void> deleteFile(@PathVariable String datasetId,@PathVariable String documentId) {
        return toAjax(difyService.delFile(datasetId,documentId));
    }

    /**
     * 查询知识库文档列表
     */
    @GetMapping("/datasets/{dataSetId}/documents")
    public DifyKnowledgeFileVo fileList(@PathVariable String dataSetId, String keyword, Integer page, Integer limit) {
        return difyService.fileList(dataSetId,keyword,page, limit);
    }

    /**
     * 新增分段
     */
    @PostMapping("/datasets/{datasetId}/documents/{documentId}/segments")
    public DifySegmentsVo segments(@PathVariable String datasetId,@PathVariable String documentId, @Validated @RequestBody DifySegmentsBo bo) {
        return difyService.segments(datasetId,documentId,bo);
    }

    /**
     * 查询文档分段
     */
    @GetMapping("/datasets/{datasetId}/documents/{documentId}/segments")
    public DifySegmentsVo segments(@PathVariable String datasetId,@PathVariable String documentId,  String keyword, String status) {
        return difyService.segments(datasetId,documentId,keyword,status);
    }

    /**
     * 删除文档分段
     */
    @DeleteMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}")
    public R<Void> segments(@PathVariable String datasetId,@PathVariable String documentId,@PathVariable String segmentId) {
        return toAjax(difyService.segments(datasetId,documentId,segmentId));
    }

    /**
     * 删除文档分段
     */
    @PostMapping("/datasets/{datasetId}/documents/{documentId}/segments/{segmentId}")
    public DifySegmentsVo segments(@PathVariable String datasetId,@PathVariable String documentId,@PathVariable String segmentId,@Validated @RequestBody DifyUpdateSegmentsBo bo) {
        return difyService.segments(datasetId,documentId,segmentId,bo);
    }

    /**
     * 知识库应用问答
     */
    @PostMapping(value = "/chat-messages", produces = MediaType.TEXT_EVENT_STREAM_VALUE)
    public  Flux<String> chat(@Validated @RequestBody DifyChatBo bo) {
        return difyService.chat(bo);
    }



}
